<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="选择出库单"
    style="min-width: 80%"
    draggable
    @close="closeDialog"
    class="maxhDialog"
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="出库单号">
          <el-input
            v-model="searchParam.outboundDeliveryCode"
            placeholder="请输入出库单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!-- <el-form-item label="领料单名称">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入领料单名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <div class="formTable">
        <el-table
          :data="tableData"
          style="width: 100%"
          :key="Math.random"
          max-height="450"
          highlight-current-row
          @current-change="handleCurrentChange1"
          v-loading="loading"
          border
        >
          <el-table-column
            label="出库单号"
            prop="outboundDeliveryCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <!-- <el-table-column
            label="领料单名称"
            prop="requisitionName"
            min-width="100"
            show-overflow-tooltip
            align="left"
          >
          </el-table-column> -->
          <el-table-column
            label="设备"
            prop="deviceCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />

          <el-table-column
            label="领料单号"
            prop="requisitionCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="出库状态"
            align="center"
            prop="isExecuted"
            min-width="100"
            show-overflow-tooltip
          >
            <template #default="scope">
              <el-tag type="info" v-if="scope.row.isExecuted === 0"
                >未出库</el-tag
              >
              <el-tag type="success" v-if="scope.row.isExecuted === 1"
                >已出库</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            label="出库类型"
            align="left"
            prop="deliveryType"
            min-width="100"
            show-overflow-tooltip
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.deliveryType == 0
                    ? 'success'
                    : scope.row.deliveryType == 1
                    ? 'danger'
                    : 'warning'
                "
                >{{
                  outboundTypeList.find(
                    (item: any) =>
                      item.outboundTypeCode == scope.row.deliveryType
                  )?.outboundTypeName ?? "维修领料出库"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            label="出库时间"
            prop="outTime"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
        </el-table>
        <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定 </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();
defineOptions({
  name: "NPMWREQUISITION",
  inheritAttrs: false,
});

onMounted(() => {
  getOutboundType();
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

//获取表格数据 api 方法
const requestApi = "/npm/wmsoutbounddelivery/queryByPageForInStore";
//获取数据初始化参数
const initParam = {};
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {
  materialName: "",
  materialCode: "",
  isExecuted: "1",
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any, parentIndex = "") => {
  // data.forEach((item: any, index: number) => {
  //   const currentIndex =
  //     parentIndex === ""
  //       ? (index + 1).toString()
  //       : `${parentIndex}.${index + 1}`;
  //   item.parentIndex = currentIndex;
  //   if (item.children && item.children.length > 0) {
  //     dataCallBack(item.children, currentIndex);
  //   }
  // });
  return data;
};

// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
/** 弹窗显示 **/
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);
/** 条件时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    if (newVal.materialCode) {
      searchParam.value.materialCode = newVal.materialCode;
      searchParamInit.materialCode = newVal.materialCode;
      search();
    }
  }
);

// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  handelRest();
}

const handelRest = () => {
  searchParam.value.materialName = "";
  searchParam.value.materialCode = "";
  // search();
};

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择领料单");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {
  getTableList();
  // getMdmMaterialList();
  getMakerList();
});

// 获取制造商列表
const makerList: any = ref([]);
const getMakerList = async () => {
  request({
    url: `/npm/mdmpartner/getOptionList`,
    method: "post",
    data: {},
  })
    .then((res) => {
      makerList.value = res.data;
    })
    .catch((res) => {
      console.log(res);
    });
};

let outboundTypeList: any = ref([]);
async function getOutboundType() {
  const data = await request({
    url: "/npm/wmsoutboundtype/getOutboundTypeList",
    method: "post",
  });
  outboundTypeList.value = data.data;
}
</script>

<style scoped lang="scss">
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
:deep(.maxhDialog .el-dialog__body) {
  max-height: 600px;
  overflow-y: auto;
  padding: 20px;
}
</style>
